﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Models;
using XiaoYuStock.Model.Request;
using XiaoYuStockAnalysis.BusinessLogic.Business;
using XiaoYuStock.Core;
using XiaoYuStock.Core.Entities;
using XiaoYuStock.AppService;

namespace XiaoYuStock.WebApp.Controllers
{
    public class ShareTargetController : BaseController
    {
        private ShareTargetBusiness shareBus = new ShareTargetBusiness();
        //
        // GET: /Book/

        public ActionResult Index(ShareTargetRequest request)
        {
            var result = shareBus.GetList(request);

            return View(result);
        }
        public ActionResult Edit(int id)
        {
            var model = shareBus.Get(id);
            return View(model);
        }


        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var model = shareBus.GetById(id);
            model.HighestPrice = Convert.ToDecimal(collection["HighestPrice"]);
            model.LowestPrice = Convert.ToDecimal(collection["LowestPrice"]);
            model.IsOpenCheck = Convert.ToBoolean(collection["IsOpenCheck"]);
            try
            {
                shareBus.Save(model);
            }
            catch (Exception e)
            {
                return View("Edit", model);
            }

            return this.RefreshParent();
        }
        [HttpPost]
        public ActionResult Delete(List<int> ids)
        {

            shareBus.Delete(ids);
            return RedirectToAction("Index");
        }
        public ActionResult Refresh()
        {
            ShareTargetRequest request = new ShareTargetRequest();
            List<TransferData> mode = new List<TransferData>();
            ShareTargetBusiness shareBus = new ShareTargetBusiness();
            request.PageSize = 20;
            var result = shareBus.GetList(request);
            if (result.Any())
            {
                var sharecodeList = new List<string>();
                result.ToList().ForEach(m => sharecodeList.Add(m.ShareType + m.ShareCode));
                var values = string.Join(",", sharecodeList);
                mode = TransferDataHelper.LoadTransferDataItems(values);
            }

            return View(mode);


        }
    }
}
